package utstat.listeners;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import utstat.UtEvent;
import utstat.UtEventListener;
import static utstat.Type.*;
/**
 * 3 knife kills in a game
 */
public class TeamworkListener implements UtEventListener {

	private Map<String, List<HitTime>> hits = new HashMap<String, List<HitTime>>();

	private int currentRound = -1;

	@Override
	public String getIntestingFact(String player) {
		return null;
	}

	@Override
	public void processEvent(UtEvent e) {
		if (e.getRound() != currentRound) {
			nextRound();
			currentRound = e.getRound();
		}
		if(e.getSrc()==null) return;
		if(e.getType()==hit) {
			
			List<HitTime> attackers = hits.get(e.getDest());
			if(attackers==null) attackers = new ArrayList<HitTime>();
			HitTime newHit = new HitTime(e.getSrc(),e.getTime());	
			if(!attackers.contains(newHit)){ attackers.add(newHit);
			hits.put(e.getDest(), attackers);
		}
		}
		if(e.getType()==kill) {
			List<HitTime> attackers = hits.get(e.getDest());
			if(attackers!=null) {
				int recentHits = 0;
				for(HitTime h : attackers) if(e.getTime()-h.time<10) recentHits++; 
				if(recentHits>2) System.out.println(e.getTeam()+" team "+recentHits+"-way'd "+e.getDest()+"!");}
			 hits.remove(e.getDest());
		}
		
	}


	private int increment(UtEvent e, Map<String, Integer> countMap) {
		Integer count = countMap.get(e.getSrc());
		if (count == null) count = 0;
		count++;
		countMap.put(e.getSrc(), count);
		return count;
	}

	private void nextRound() {
		System.out.println("---------");
		hits = new HashMap<String, List<HitTime>>();
	}
	
	
	class HitTime{
		@Override
		public boolean equals(Object obj) {
			HitTime that = (HitTime)obj;
			return this.src.equals(that.src);
		}

		@Override
		public int hashCode() {
			return src.hashCode();
		}

public HitTime(String src, int time) {
			this.src=src;this.time=time;
		}
		public String src;
		public int time; }

}
